//
//  CustomSectionView.m
//  LearnToHelp
//
//  Created by 杨旭 on 2017/11/7.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXCustomSectionView.h"

@interface YXCustomSectionView ()

@property (nonatomic ,strong) NSArray *titleArr;
@property (nonatomic ,strong) NSArray *imageArr;
@property (nonatomic, strong) UIButton *selectedBtn;
@property (nonatomic, strong) UIView *flagView;

@end

@implementation YXCustomSectionView

- (instancetype)initWithFrame:(CGRect)frame withTieleArr:(NSArray *)titleArr withImageArr:(NSArray *)imageArr
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.titleArr = titleArr;
        self.imageArr = imageArr;
        [self setup];
    }
    return self;
}


- (void)setup
{
    
    for (int i = 0 ; i < self.titleArr.count; i ++ ) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        btn.frame = CGRectMake(kScreenWidth / self.titleArr.count * i, 0, kScreenWidth / self.titleArr.count, self.frame.size.height);
        [btn setTitle:self.titleArr[i] forState:(UIControlStateNormal)];
        [btn setTitleColor:color_textThree forState:(UIControlStateNormal)];
        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.imageArr[i]]] forState:(UIControlStateNormal)];
        btn.tag = 200 + i;
        btn.titleLabel.font = [UIFont systemFontOfSize:17];
        [btn setImgViewStyle:(ButtonImgViewStyleLeft) imageSize:(CGSizeMake(20, 20)) space:8
         ];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            
            self.selectedBtn = btn;
            [btn setTitleColor:color_globalColor forState:UIControlStateNormal];
            [self createFlagViewWithFrame:CGRectMake(30, self.frame.size.height-20, (kScreenWidth - 60 * self.titleArr.count) / self.titleArr.count, 2)];
        }
        
        [self addSubview:btn];
        

    }
    
    UIView *lineView = [[UIView alloc] initWithFrame:(CGRectMake(0,self.frame.size.height-1, kScreenWidth, 1))];
    lineView.backgroundColor = color_lineColor;
    [self addSubview:lineView];


}

- (void)btnAction:(UIButton *)sender {
    if (self.selectedBtn != sender) {
        
        [sender setTitleColor:color_globalColor forState:UIControlStateNormal];
        [self.selectedBtn setTitleColor:color_textOne forState:UIControlStateNormal];
    }
    self.selectedBtn = sender;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.flagView.frame  = CGRectMake(sender.frame.origin.x + 30, sender.frame.size.height - 20, sender.frame.size.width - 60, 2);
    }];

    self.selectBtnClick(sender.tag -200);

}
- (void)createFlagViewWithFrame:(CGRect)frame  {
    
    self.flagView = [[UIView alloc] initWithFrame:frame];
    self.flagView.backgroundColor = color_globalColor;
    [self addSubview:self.flagView];
    
}



@end
